//
//  JYJSepcialListTableViewCell.m
//  HappyListen
//
//  Created by 冀永金 on 16/8/30.
//  Copyright © 2016年 冀永金. All rights reserved.
//

#import "JYJSepcialListTableViewCell.h"
#import "BaseLabel.h"
#import "Masonry.h"
#import "BaseCollectionView.h"
#import "JYJTAMacro.h"
#import "JYJSepciaListCollectionCell.h"
#import "JYJNetWorkParser.h"
#import "JYJSpecialListModel.h"
#import "JYJVCOfAnchorInfoViewController.h"
@interface JYJSepcialListTableViewCell ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic, strong) UIImageView *image;
@property (nonatomic, strong) UILabel *label;
@property (nonatomic, strong) BaseCollectionView *collection;
@property (nonatomic, strong) UICollectionViewFlowLayout *layout;
@property (nonatomic, strong) NSMutableArray<JYJSpecialListModel  *> *marrOfModel;
@end
@implementation JYJSepcialListTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self configViews];
        [self handleData];
    }
    return self;
}
- (void)configViews {
    self.image = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"find_cat_header"]];
    [self.contentView addSubview:_image];
    self.label = [[BaseLabel alloc]init];
    [self.contentView addSubview:_label];
    _label.text = @"人气主播";
    _label.textColor = [UIColor whiteColor];
    _layout = [[UICollectionViewFlowLayout alloc]init];
    self.collection = [[BaseCollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:_layout];
    [self.contentView addSubview:_collection];
    self.collection.delegate = self;
    self.collection.dataSource = self;
    [self.collection registerClass:[JYJSepciaListCollectionCell class] forCellWithReuseIdentifier:@"Special"];
    self.collection.scrollEnabled = NO;
}
- (void)handleData {
    [JYJNetWorkParser POST:@"http://www.aiyinsitanfm.com/mobile/channel/discovery.jhtml" andBody:@"json=%7B%7D" andBodyStyle:JYJBodyString andHeader:HEADDIC andResponse:JYJJSON andSuccessBlock:^(id result) {
       
        self.marrOfModel = [NSMutableArray array];
        NSArray *listArray = result[@"data"][@"special_list"];
        for (NSDictionary *dic in listArray) {
            JYJSpecialListModel *model = [JYJSpecialListModel modelWithDic:dic];
            [self.marrOfModel addObject:model];
        }
        [self.collection reloadData];
    } andFailureBlock:^(NSError *error) {
        
    }];

}
- (void)layoutSubviews {
    [super layoutSubviews];
    
    _layout.itemSize = CGSizeMake((SCREEN_WIDTH - 50) / 4, (SCREEN_WIDTH - 50) / 2.5);
    _layout.sectionInset = UIEdgeInsetsMake(10, 10, 5, 10);
    _layout.minimumLineSpacing = 10;
    
    self.collection.frame = CGRectMake(0, 50, SCREEN_WIDTH, self.bounds.size.height - 50);
    [self.image mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView.mas_top).with.offset(10);
        make.left.equalTo(self.contentView.mas_left).with.offset(10);
        make.width.mas_equalTo(35);
        make.height.mas_equalTo(35);
    }];
    [self.label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView.mas_top).with.offset(13);
        make.left.equalTo(self.image.mas_right).with.offset(5);
        make.width.mas_equalTo (100);
        make.height.mas_equalTo(30);
    }];

}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.marrOfModel.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    JYJSepciaListCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Special" forIndexPath:indexPath];
   
    JYJSpecialListModel *model = self.marrOfModel[indexPath.row];
    [cell setSpecialModel:model];
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    JYJVCOfAnchorInfoViewController *info = [[JYJVCOfAnchorInfoViewController alloc]init];
    JYJSpecialListModel *model = self.marrOfModel[indexPath.row];
    info.listModel = model;
    self.block(info);
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
